package com.jl.controller;

import com.jl.commons.base.BaseController;
import com.jl.commons.constant.Constants;
import com.jl.commons.constant.ErrorCode;
import com.jl.commons.constant.ErrorMessage;
import com.jl.commons.result.PageDataInfo;
import com.jl.commons.result.ResultData;
import com.jl.service.IDamagePartService;
import com.jl.service.IDeviceComponentService;
import com.jl.service.IDeviceService;
import com.jl.service.IFaultInfoService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 零件表 前端控制器
 * </p>
 *
 * @author JiangJian
 * @since 2019-03-06
 */
@Controller
@RequestMapping("/home")
public class HomeController extends BaseController {

    @Autowired
    private IDeviceComponentService deviceComponentService;
    @Autowired
    private IFaultInfoService faultInfoService;
    @Autowired
    private IDamagePartService damagePartService;
    @Autowired
    private IDeviceService deviceService;

    /**
     * 获取快到寿命的的零件
     */
    @PostMapping("/deadLineComponentDataGrid")
    @ResponseBody
    public ResultData deadLineComponentDataGrid(Long current,Long pageSize,String searchCol,String searchValue){
        if(current == null || pageSize == null){
            return fail(ErrorCode.PAGE_PARAM_ERROR, ErrorMessage.PAGE_PARAM_ERROR);
        }
        if(StringUtils.isBlank(searchValue) ){
            searchValue = Constants.WEEK_TDAYS_LENGTH_STR;
        }
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        pageDataInfo.setCurrent(current);
        pageDataInfo.setSize(pageSize);
        deviceComponentService.getDeadLineComponent(pageDataInfo,Integer.parseInt(searchValue));
        return success(pageDataInfo);
    }

    @PostMapping("/faultSymptom")
    @ResponseBody
    public ResultData repeatFaultSymptom(String startTime,String endTime){
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        faultInfoService.getRepeatFaultSymptom(pageDataInfo,startTime,endTime);
        return success(pageDataInfo);
    }

    @PostMapping("/repeatDamagePart")
    @ResponseBody
    public ResultData repeatDamagePart(String startTime,String endTime){
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        damagePartService.getRepeatDamagePart(pageDataInfo,startTime,endTime);
        return success(pageDataInfo);
    }

    @GetMapping("/repeatDamageDevice")
    public String repeatDamageDevicePage(ModelMap modelMap){
        modelMap.put("damageDevice",1);
        return "home";
    }

    @PostMapping("/repeatDamageDevice")
    @ResponseBody
    public ResultData repeatDamageDevice(Long current,Long pageSize,String startTime,String endTime){
        if(current == null || pageSize == null){
            return fail(ErrorCode.PAGE_PARAM_ERROR, ErrorMessage.PAGE_PARAM_ERROR);
        }
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        pageDataInfo.setCurrent(current);
        pageDataInfo.setSize(pageSize);
        deviceService.getRepeatDamageDeviceInMonth(pageDataInfo,startTime,endTime);
        return success(pageDataInfo);
    }

}
